package com.check.operation.fragment;

import android.content.DialogInterface;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.check.operation.CheckApplication;
import com.check.operation.R;
import com.check.operation.base.BaseFragment;
import com.check.operation.model.MessagRe;
import com.check.operation.model.Message;
import com.check.operation.model.RequestUse;
import com.check.operation.model.Ssroom;
import com.check.operation.model.Surgictz;
import com.check.operation.service.SstzAdapter;
import com.check.operation.widget.DateTimePickerDialog;
import com.szj.util.HttpUtil;
import com.szj.util.JSONUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 手术通知
 */
@EFragment(R.layout.fragment_sstz)
public class SstzFragment extends BaseFragment implements AdapterView.OnItemClickListener, View.OnClickListener, AdapterView.OnItemSelectedListener {
    public static final String TAG = "Sstz";

    @App
    CheckApplication sshcApp;
    @ViewById
    ListView sqlb;
    @ViewById
    EditText dateedit;
    @ViewById
    Spinner ssfj;
    @ViewById
    ImageButton search;

    final String sszt = "i";
    final String ssztmc = "手术通知";

    ArrayAdapter<Ssroom> ssfjop;
    SstzAdapter sstzAdapter;
    List<Surgictz> surgictzs;
    Ssroom ssroom;

    @AfterViews
    public void initview() {
        ssfjop = new ArrayAdapter<Ssroom>(getActivity(), android.R.layout.simple_spinner_item);
        ssfjop.addAll(sshcApp.getSsrooms());
        ssfj.setAdapter(ssfjop);
        ssfj.setOnItemSelectedListener(this);
        sstzAdapter = new SstzAdapter(getLayoutInflater(),sshcApp.getUserRe().getUser());

        dateedit.setText(JSONUtil.getNYR(sshcApp.getDate()));
        dateedit.setOnClickListener(new DateOnClick());
        search.setOnClickListener(this);
        sqlb.setOnItemClickListener(this);
    }

    @Background
    public void refdownsj() {
        try {
            String action = "surgicreg/surgicapplys";
            Map<String,Object> params=new HashMap<String, Object>();
            //params.put("user",sshcApp.getUserRe().getUser());
            params.put("date",dateedit.getText().toString());
            params.put("room",ssroom.code);
            RequestUse requestUse=new RequestUse();
            requestUse.user=sshcApp.getUserRe().getUser();
            requestUse.request=params;
            String rs = HttpUtil.postJSON(action, JSONObject.toJSONString(requestUse));
            MessagRe<List<Surgictz>> stzs=JSONObject.parseObject(rs, new TypeReference<MessagRe<List<Surgictz>>>(){});
            //operations=inp.value.operations;
            if("success".equals(stzs.message.code)){
                //	sstzAdapter.setCws(stzs.value);
                surgictzs=stzs.value;
                uiSugtz(stzs.value);
            }else{
                initlist();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @UiThread
    public void initlist(){
        try {
            sstzAdapter.setCws(new ArrayList<Surgictz>());
            sqlb.setAdapter(sstzAdapter);
            sstzAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @UiThread
    public void uiSugtz(List<Surgictz> tzs) {
        try {
            sstzAdapter.setCws(tzs);
            sqlb.setAdapter(sstzAdapter);
            sstzAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @UiThread
    public void alert(Message message) {
        Toast.makeText(getActivity(), message.text, Toast.LENGTH_SHORT).show();
    }

    @UiThread
    public void alert(String message) {
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2,long arg3) {
        try {
            sstzAdapter.setIndex(arg2);
            sstzAdapter.notifyDataSetInvalidated();
            final Surgictz operation = surgictzs.get(arg2);
            new AlertDialog.Builder(getActivity())
                    .setTitle(ssztmc + "？")
                    .setPositiveButton("是", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String action = "notification/notify";
                            RequestUse requestUse = new RequestUse();
                            requestUse.user = sshcApp.getUserRe().getUser();
                            Map<String,Object> params=new HashMap<String, Object>();
                            params.put("surgicNo", operation.surgic_no);
                            params.put("type", sszt);
                            requestUse.request = params;
                            String rs = HttpUtil.postJSON(action,JSONObject.toJSONString(requestUse));
                            TextView t1 = (TextView) arg1.findViewById(R.id.sszt);
                            t1.setText(ssztmc);
                        }
                    })
                    .setNegativeButton("否", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {}
                    }).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetBarValue(String paramString) {

    }


    private final class DateOnClick implements View.OnClickListener {
        public void onClick(View v) {
            DateTimePickerDialog dateTimePicKDialog = new DateTimePickerDialog(getActivity());
            dateTimePicKDialog.dateTimePicKDialog(dateedit, 1);
        }
    }


    @Override
    public void onClick(View v) {
        refdownsj();
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        ssroom=sshcApp.getSsrooms().get(position);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        ssroom=null;
    }
}
